home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cppmatrx / matparse.c < prev    next >
Text File  |  1991-02-02  |  1KB  |  77 lines

  1. #include <math.h>
  2. #include <iostream.h>
  3.  
  4. #include "matrix.h"
  5. #include "matparse.h"
  6.  
  7. static char current_token;
  8.  
  9. matrix expression (void)
  10. {
  11.    matrix left = term ();
  12.    matrix right ("*",2);
  13.    matrix temp ("*",2);
  14.  
  15.    while (1)   {
  16.          switch (current_token)   {
  17.                case '+':
  18.                   right = term ();
  19.                   temp = left+right;
  20.                   left = temp;  
  21.                   break;
  22.  
  23.                case '-':
  24.                   right = term ();
  25.                   temp = left-right;
  26.                   left = temp;
  27.                   break;  
  28.  
  29.                case '!':
  30.                   return left;
  31.  
  32.                default:
  33.                   matrix_error ("Invalid character in expression.");
  34.             }
  35.       }
  36. }
  37.  
  38.  
  39. matrix term (void)
  40. {
  41.    matrix left = primary ();
  42.    matrix right ("*",2);
  43.    matrix temp ("*",2);
  44.  
  45.    while (1)   {
  46.          switch (get_token ())   {
  47.                case '*':
  48.                   right = primary (); 
  49.                   temp = left*right;
  50.                   left = temp;
  51.                   break;
  52.  
  53.                default:
  54.                   return left;
  55.             }
  56.  
  57.       }
  58. }
  59.  
  60. matrix  primary (void)
  61. {
  62.    char ch;
  63.    matrix temp ("*",2);
  64.  
  65.    cin >> ch;
  66.    cin >> temp;
  67.    cin >> ch;
  68.  
  69.    return temp;
  70. }
  71.  
  72. char get_token (void)
  73. {
  74.    cin >> current_token;
  75.    return current_token;
  76. }
  77.